﻿using UnityEngine;
using System.Collections;

namespace SG
{
    public class NativeAndroid
    {
#if UNITY_ANDROID
        private static string JAVA_CLASS = "com.unity3d.player.UnityPlayer";
#endif

        public static void CallMethod(string method, params object[] args)
        {
#if UNITY_ANDROID
        using (AndroidJavaClass agent = new AndroidJavaClass(JAVA_CLASS))
        {
            AndroidJavaObject jo = agent.GetStatic<AndroidJavaObject>("currentActivity");
            jo.Call(method, args);
            Debug.Log("AndroidJavaClass ----->  Method [" + method + "] called");
        }
#endif
        }

        public static T CallMethod<T>(string method, params object[] args)
        {
            T returnValue = default(T);
#if UNITY_ANDROID
            using (AndroidJavaClass agent = new AndroidJavaClass(JAVA_CLASS))
            {
                AndroidJavaObject jo = agent.GetStatic<AndroidJavaObject>("currentActivity");
                returnValue = jo.Call<T>(method, args);
                Debug.Log("AndroidJavaClass ----->  Method [" + method + "] called" + " returnValue is " + returnValue.ToString());
            }
#endif

            return returnValue;
        }

        static public void CallMethodStatic(string method, params object[] args)
        {
#if UNITY_ANDROID
            using (AndroidJavaClass agent = new AndroidJavaClass(JAVA_CLASS))
            {
                AndroidJavaObject jo = agent.GetStatic<AndroidJavaObject>("currentActivity");
                jo.CallStatic(method, args);
                Debug.Log("AndroidJavaClass ----->  Static Method [" + method + "] called");
            }
#endif
        }

        public static T CallMethodStatic<T>(string method, params object[] args)
        {
            T returnValue = default(T);

#if UNITY_ANDROID
            using (AndroidJavaClass agent = new AndroidJavaClass(JAVA_CLASS))
            {
                AndroidJavaObject jo = agent.GetStatic<AndroidJavaObject>("currentActivity");
                returnValue = jo.CallStatic<T>(method, args);
                Debug.Log("AndroidJavaClass ----->  Static Method [" + method + "] called" + " returnValue is " + returnValue.ToString());
            }
#endif

            return returnValue;
        }

        protected FieldType GetField<FieldType>(string fiedName)
        {
#if UNITY_ANDROID
        using (AndroidJavaClass agent = new AndroidJavaClass(JAVA_CLASS))
        {
            AndroidJavaObject jo = agent.GetStatic<AndroidJavaObject>("currentActivity");
            return jo.Get<FieldType>(fiedName);
        }
#endif
            return default(FieldType);
        }

        protected FieldType GetFieldStatic<FieldType>(string fiedName)
        {
#if UNITY_ANDROID
        using (AndroidJavaClass agent = new AndroidJavaClass(JAVA_CLASS))
        {
            AndroidJavaObject jo = agent.GetStatic<AndroidJavaObject>("currentActivity");
            return jo.GetStatic<FieldType>(fiedName);
        }
#endif
            return default(FieldType);
        }

        protected void SetField<FieldType>(string fiedName, FieldType val)
        {
#if UNITY_ANDROID
        using (AndroidJavaClass agent = new AndroidJavaClass(JAVA_CLASS))
        {
            AndroidJavaObject jo = agent.GetStatic<AndroidJavaObject>("currentActivity");
            jo.Set<FieldType>(fiedName,val);
        }
#endif
        }

        protected void SetFieldStatic<FieldType>(string fiedName, FieldType val)
        {
#if UNITY_ANDROID
        using (AndroidJavaClass agent = new AndroidJavaClass(JAVA_CLASS))
        {
            AndroidJavaObject jo = agent.GetStatic<AndroidJavaObject>("currentActivity");
            jo.SetStatic<FieldType>(fiedName,val);
        }
#endif
        }
    }
}